package com.useeinfo.asg.open.admin;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.ResourceBiz;
import com.useeinfo.asg.modules.logging.entity.Resource;
import com.useeinfo.asg.modules.security.entity.SystemUser;
import com.useeinfo.asg.open.web.CommUserUtil;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.file.FileUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by GeniusJ on 2017-05-12.
 */
@Controller
@RequestMapping("/admin/resource")
public class ResourceAdminManageAction {

    @Autowired
    private ResourceBiz resourceBiz;

    @RequestMapping("/list")
         public String list(HttpServletRequest request, ModelMap mv) {

        return "/admin/resource/list";
    }

    /**
     * 分页显示材料列表
     * @param projectId 项目于id
     * @param page 当前页
     * @param rows 显示行数
     * */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(String projectId,String page,String rows) {

        JSONObject jsonRet=CommUtil.jsonResult(true);

        QueryParam queryParam=new QueryParam(page,rows);
/*
        queryParam.getSqlMap().put("project",projectId);

        queryParam.getSqlMap().put("max","3");*/

        return  resourceBiz.findJSONList(queryParam);
    }

    /**
     * 文件上传
     * @param request 请求
     * @param uploadFile 文件
     * */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public JSONObject fileUpload(HttpServletRequest request,MultipartFile uploadFile,String name)
    {
        JSONObject retObject= CommUtil.jsonResult(false);

        SystemUser user= CommUserUtil.getUser(request);
        Resource resource=new Resource();

//获取文件格式
        String subFix = uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().lastIndexOf(".")).toLowerCase();

        //获取上传后的路径
//            String filePath=FileUtils.fileUpload(uploadFile,getStuff(stuffType),name);
        String filePath=FileUtils.fileUpload(uploadFile,"","");
            resource.setResType(getResType(subFix));
            resource.setCreateTime(new Date());
            resource.setResPath(filePath);
            resource.setResName(name);
            resourceBiz.addOrUpdate(resource);

        retObject=CommUtil.jsonResult(true);
        return retObject;
    }

    /**
     * 假删
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(String id){

        JSONObject retObject=CommUtil.jsonResult(false);

        Resource resource = null;
        if (CommUtil.null2Long(id)>0 && (resource = resourceBiz.findModel(CommUtil.null2Long(id)))!=null) {
            if (resourceBiz.delete_logic(resource)>0)
                return CommUtil.jsonResult(true);
        }
        return  retObject;
    }
    @RequestMapping("/look")
    public String  look(HttpServletRequest request,String id,ModelMap modelMap){


        Resource resource = resourceBiz.findModel(CommUtil.null2Long(id));
        modelMap.put("url",CommUtil.getURL(request)+resource.getResPath());
        return "/admin/resource/edit";
    }

    /**
     * 做文件格式的判断
     * @param subFix 文件类型 如：.png .jpg
     * @return 类型
     * */
    private int getResType(String subFix) {
        switch (subFix){
            case ".mp4":
                return 1;
            case ".mov":
                return 1;
            case ".avi":
                return 1;
            case ".pdf":
                return 2;
            case ".doc":
                return 3;
            case ".docx":
                return 3;
            case ".xls":
                return 4;
            case ".xlsx":
                return 4;
            case ".ppt" :
                return 5;
            case ".pptx":
                return 5;
            case ".png":
                return 6;
            case ".jpg":
                return 6;
            case ".gif":
                return 6;
            default:
                return 0;
        }
    }
}
